Skip to content

Optimize context usage for get_files in pull_request_read#2059

Merged
tommaso-moro merged 2 commits intomainfrom
tommy/optimize-tools-response-size
Feb 23, 2026
Merged

Optimize context usage for get_files in pull_request_read#2059
tommaso-moro merged 2 commits intomainfrom
tommy/optimize-tools-response-size

Conversation

@tommaso-moro
Copy link
Contributor

@tommaso-moro tommaso-moro commented Feb 23, 2026

Closes: https://github.com/github/copilot-mcp-core/issues/1309

Summary

This PR reduces the context window usage when listing pull request files using the pull_request_read tool (get_pull_request_files).

It does so by using the minimal types pattern (which is already used elsewhere in the codebase for the same reason) to reduce the payload that is sent back to the model when the tool is used. Specifically, irrelevant URL fields are removed and pointer-wrapped primitives are flattened to value types, eliminating unnecessary JSON noise per file entry.

Tests & Metrics

When tested with this PR:
Before: 6125 tokens
After: 5077 tokens
Context reduction: 17.11%

Fields preserved

filename, status, additions, deletions, changes, patch, previous_filename

Fields dropped

sha (blob SHA, not useful for reasoning), blob_url, raw_url, contents_url (API URL strings, never used by models)

Why

The full github.CommitFile payload returned by the GitHub API includes several fields that are irrelevant for model reasoning — most notably three API URL strings (blob_url, raw_url, contents_url) and a blob sha per file. These add up quickly for PRs with many changed files and waste context window on every call. Additionally, the raw Go types use pointer-wrapped primitives (*string, *int) which produce noisier JSON than plain value types.

What changed

  • Added MinimalPRFile type to minimal_types.go, following the existing MinimalCommitFile/MinimalPullRequest pattern — includes patch and previous_filename which are relevant for PR file diffs but not present in MinimalCommitFile
  • Added convertToMinimalPRFiles converter function
  • Updated GetPullRequestFiles to return []MinimalPRFile via MarshalledTextResult instead of raw json.Marshal(files)
  • Updated tests to assert against MinimalPRFile fields

MCP impact

  • No tool or API changes
  • Tool schema or behavior changed
  • New tool added

Security / limits

  • No security or limits impact
  • Auth / permissions considered
  • Data exposure, filtering, or token/size limits considered

Tool renaming

  • I am renaming tools as part of this PR (e.g. a part of a consolidation effort)
    • I have added the new tool aliases in deprecated_tool_aliases.go
  • I am not renaming tools as part of this PR

Note: if you're renaming tools, you must add the tool aliases. For more information on how to do so, please refer to the official docs.

Lint & tests

  • Linted locally with ./script/lint
  • Tested locally with ./script/test

Docs

  • Not needed
  • Updated (README / docs / examples)

@tommaso-moro tommaso-moro marked this pull request as ready for review February 23, 2026 10:52
@tommaso-moro tommaso-moro requested a review from a team as a code owner February 23, 2026 10:52
Copilot AI review requested due to automatic review settings February 23, 2026 10:52
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes context/token usage for the get_files method in the pull_request_read tool by introducing a new MinimalPRFile type that reduces payload size compared to the full github.CommitFile type. The change follows the established pattern in the codebase of using minimal types (similar to MinimalIssue, MinimalPullRequest, MinimalCommitFile, etc.) to reduce token consumption while preserving essential information.

Changes:

  • Introduced MinimalPRFile type with essential PR file fields (filename, status, additions, deletions, changes, patch, previous_filename)
  • Added convertToMinimalPRFiles conversion function to transform GitHub API responses to the minimal type
  • Updated GetPullRequestFiles to use the new minimal type and MarshalledTextResult helper
  • Updated test expectations to validate the minimal type output

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
pkg/github/minimal_types.go Added MinimalPRFile type and convertToMinimalPRFiles conversion function
pkg/github/pullrequests.go Updated GetPullRequestFiles to use minimal type conversion
pkg/github/pullrequests_test.go Updated test to validate MinimalPRFile output instead of full CommitFile

@tommaso-moro tommaso-moro merged commit 713848b into main Feb 23, 2026
16 checks passed
@tommaso-moro tommaso-moro deleted the tommy/optimize-tools-response-size branch February 23, 2026 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants